package asionreachel.cn.smarthomedemo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import asionreachel.cn.smarthomedemo.R;
import asionreachel.cn.smarthomedemo.base.BaseActivity;
import asionreachel.cn.smarthomedemo.base.Common;
import asionreachel.cn.smarthomedemo.monitor.JLoginMonitor;
import familyclient.android.loginmanager.JLoginManagerInterface;
import familyclient.android.loginmanager.JLoginManagerListener;
import familyclient.android.netswitcher.JNetSwitcherInterface;

/**
 * Created by AsionReachel on 2018/3/9.
 */

public class PersonalCenterActivify extends BaseActivity implements View.OnClickListener{

    TextView toForgetpwd,unlogin,back;
    private static final String FUNCTION = "Login";

    private JLoginManagerListener m_listenerLoginManager = new JLoginManagerListener() {
        @Override
        public void LogoutCb( String a_strErr )
        {
            if( !m_bRun )
                return;

            Common.closeDialogLoading();
            if( null != a_strErr && a_strErr.length() > 0 )
            {
                Common.showToast( Common.MakeMsgByErr( FUNCTION, a_strErr ) );
            }
            else
            {
                //登出成功
                onLogoutSuccess();
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_personnalcenter);
        JLoginManagerInterface.GetInstance().AddListener(m_listenerLoginManager);
        toForgetpwd=findViewById(R.id.toforgotpwd);
        unlogin=findViewById(R.id.unlogin);
        back=findViewById(R.id.back);
        back.setOnClickListener(this);
        toForgetpwd.setOnClickListener(this);
        unlogin.setOnClickListener(this);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.toforgotpwd:
                startActivity(new Intent(this,ForgotPasswordActivity.class));
                break;
            case R.id.unlogin:
                onLogout();
                break;
            case R.id.back:
                finish();
                break;
        }
    }
    //登出
    private void onLogout()
    {
        JLoginManagerInterface.GetInstance().Logout();
        Common.showDialogLoading(mActivity , "正在退出");
    }
    //登出成功
    private void onLogoutSuccess()
    {
        JNetSwitcherInterface.GetInstance().DisconnectInternetServer();
        JLoginMonitor.GetInstance().SetAutoLogin( false );

        Common.showToast("退出登录成功");
        Common.savePassword( "" );
        Intent intent = new Intent(mActivity , LoginActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    protected void onDestroy()
    {
        JLoginManagerInterface.GetInstance().DelListener( m_listenerLoginManager );
        super.onDestroy();
    }

    @Override
    public void onBackKey()
    {
       finish();
    }

}
